<template>
  <div class="model-container">
    <div class="listName">会员列表</div>
    <!--搜索-->
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>快捷搜索</span>
        <div class="btns-wrapper-right">
          <el-button v-if="!searchShow" icon="el-icon-arrow-down" @click="searchShow = true"/>
          <el-button v-else icon="el-icon-arrow-up" @click="searchShow = false"/>
        </div>
      </div>
      <el-collapse-transition>
        <div v-show="searchShow" class="cardbody">
          <el-form ref="seacherParam" :model="seacherParam">
            <el-row :gutter="10">
              <el-col :xl="5" :lg="6" :md="6" :sm="8" >
                <el-col :xl="7" :lg="7" :md="8" :sm="8" class="input-name">会员昵称</el-col>
                <el-col :xl="17" :lg="17" :md="16" :sm="16">
                  <el-form-item prop="nickName">
                    <el-input v-model="seacherParam.nickName" size="small" placeholder="请输入会员昵称" maxlength="15"/>
                  </el-form-item>
                </el-col>
              </el-col>
            </el-row>
            <div class="cardfooter">
              <el-button type="primary" size="mini" plain @click="seachFun">查询</el-button>
              <el-button type="warning" size="mini" plain @click="resetFun">重置</el-button>
            </div>
          </el-form>
        </div>
      </el-collapse-transition>
    </el-card>
    <!--表格-->
    <el-card style="margin-top: 10px">
      <div slot="header" class="clearfix">
        <div class="btns-wrapper-left">
          <el-button type="danger" plain @click="del(0)">删除</el-button>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table v-loading="!dataOption" :stripe="true" :data="dataOption" border empty-text="暂无数据" @selection-change="changeFun">

          <el-table-column align="center" type="selection" width="55"/>

          <el-table-column align="center" prop="nickName" label="会员昵称"/>

          <el-table-column align="center" prop="integral" label="会员积分"/>

          <el-table-column align="center" label="性别">
            <template slot-scope="scope">{{ genderVal(scope.row.gender) }}</template>
          </el-table-column>

          <el-table-column align="center" prop="mobilePhone" label="会员手机	"/>

          <el-table-column align="center" label="是否在黑名单	">
            <template slot-scope="scope">{{ isBlacklistVal(scope.row.isBlacklist) }}</template>
          </el-table-column>

          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">

              <el-button title="编辑" type="primary" circle icon="el-icon-edit" class="table-icon" @click="edit(scope.row)"/>

              <!--<el-button title="删除" type="warning" circle icon="el-icon-delete" class="table-icon" @click="del(scope.row)"/>-->

              <el-button v-if="scope.row.isBlacklist === 1" title="恢复" type="warning" circle icon="el-icon-refresh" class="table-icon" @click="huifu(scope.row)"/>

              <el-button v-if="scope.row.isBlacklist === 0" title="拉黑" type="warning" circle class="table-icon " @click="shielding(scope.row)"><i class="iconfont icon-lahei"/></el-button>

            </template>
          </el-table-column>

        </el-table>

        <!--分页-->
        <el-pagination
          :current-page="pageNo"
          :page-sizes="[10, 20, 30]"
          :page-size="pageSize"
          :total="rowCount"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

    <!--弹框-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogState"
      :close-on-click-modal="false"
      :before-close="handleClose"
      top="5vh"
      width="40%">
      <el-form ref="dialogBindData" :model="dialogBindData" :rules="rules" label-width="30%" status-icon class="demo-ruleForm">
        <div class="formBody">
          <el-form-item label="会员昵称" prop="nickName">
            <el-input v-model="dialogBindData.nickName" size="small" type="text" placeholder="请输入会员昵称" maxlength="15"/>
          </el-form-item>
          <el-form-item label="手机号码" prop="mobilePhone">
            <el-input v-model="dialogBindData.mobilePhone" size="small" type="text" placeholder="请输入手机号码" maxlength="11"/>
          </el-form-item>
          <el-form-item label="会员头像" prop="picUrl" class="three">
            <div class="uploadImg">
              <img :src="picUrl">
              <div class="uploadImg_mock" @click="imagecropperShow=true"><i class="iconfont icon-xiugai"/></div>
            </div>
            <image-cropper
              v-show="imagecropperShow"
              :key="imagecropperKey"
              :url="actionUrl"
              @close="close"
              @crop-upload-success="cropSuccess"/>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="dialogBindData.gender">
              <el-radio :label="0">未录入</el-radio>
              <el-radio :label="1">女</el-radio>
              <el-radio :label="2">男</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="当前积分值" prop="integral">
            <span>{{ dialogBindData.integral }}</span>
            <el-button-group style="margin-left: 20px">
              <el-button size="mini" type="primary" @click="integralFun(0)">添加</el-button>
              <el-button size="mini" type="primary" @click="integralFun(1)">减少</el-button>
            </el-button-group>
          </el-form-item>
          <el-form-item v-if="type === 0" label="增加积分值" prop="tempIntegral">
            <el-input v-model="dialogBindData.tempIntegral" size="small" type="text" placeholder="请输入增加积分值" maxlength="11"/>
          </el-form-item>
          <el-form-item v-if="type === 1" label="减少积分值" prop="tempIntegral">
            <el-input v-model="dialogBindData.tempIntegral" size="small" type="text" placeholder="请输入减少积分值" maxlength="11"/>
          </el-form-item>
        </div>
        <div class="btns-wrapper">
          <el-button size="small" @click="handleClose">取 消</el-button>
          <el-button :loading="buttonState" type="primary" size="small" @click="submmitFun">保 存</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import * as member from '../../api/member'
import ImageCropper from '@/components/ImageCropper'
export default {
  components: {
    ImageCropper
  },
  data() {
    return {
      // 页面搜索
      seacherParam: {
        nickName: ''
      },
      searchShow: false,
      pageNo: 1,
      pageSize: 10,
      rowCount: null, // 总条数
      // 弹框
      dialogType: 1, // 1--增加  2--编辑  3--查看
      dialogState: false,
      // 按钮状态
      buttonState: false,
      // 列表数据
      dataOption: null,
      datavalID: [],
      // 提交内容
      type: '',
      dialogBindData: {
        memberId: null,
        nickName: '',
        mobilePhone: '',
        gender: 0,
        loginLockStatus: 0,
        integral: '',
        tempIntegral: ''
      },
      rules: {
        nickName: [
          { required: true, message: '请输入会员昵称', trigger: 'blur' }
        ],
        mobilePhone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        tempIntegral: [
          { required: true, message: '请输入积分值', trigger: 'blur' }
        ]
      },
      // 上传头像
      actionUrl: '',
      imagecropperShow: false,
      imagecropperKey: 0,
      picUrl: ''
    }
  },
  computed: {
    dialogTitle() {
      if (this.dialogType === 1) {
        return '会员信息'
      }
      if (this.dialogType === 2) {
        return '会员信息'
      } else {
        return '查看'
      }
    }
  },
  beforeMount() {
    this.getList()
  },
  created() {
    this.initUploadFileurl()
  },
  methods: {
    initUploadFileurl() {
      this.actionUrl = process.env.BASE_API + 'file/uploadImageAndCrtThumbImagesRichEdit'
    },
    seachFun() {
      this.pageNo = 1
      this.pageSize = 10
      const url = this.searchData()
      this.getList(url)
    },
    getList(data) {
      let url = ''
      if (data !== '' && data !== undefined) {
        url = data
      }
      const param = {
        current: this.pageNo,
        size: this.pageSize,
        url: url
      }
      member.getMemberList(param).then(res => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        // 初始话表格数据
        this.dataOption = res.data.data.records
        // 初始化分页数据
        this.rowCount = res.data.data.total
        this.pageSize = res.data.data.size
      })
    },
    // 编辑
    edit(data) {
      this.dialogType = 2
      this.dialogState = true
      this.dialogBindData.memberId = data.memberId
      if (data.picUrl === null || data.picUrl === '' || data.picUrl === undefined) {
        this.picUrl = 'https://wpimg.wallstcn.com/577965b9-bb9e-4e02-9f0c-095b41417191'
      } else {
        this.picUrl = data.picUrl
      }
      this.reBindData(data)
    },
    // 提交
    submmitFun() {
      const param = {
        nickName: this.dialogBindData.nickName,
        mobilePhone: this.dialogBindData.mobilePhone,
        gender: this.dialogBindData.gender,
        picUrl: this.picUrl,
        loginLockStatus: 0
      }
      if (this.dialogType !== 1) {
        param.memberId = this.dialogBindData.memberId
      }
      if (this.type === 0) {
        param.type = 0
        param.tempIntegral = this.dialogBindData.tempIntegral
      } else {
        param.type = 1
        param.tempIntegral = this.dialogBindData.tempIntegral
      }
      this.$refs.dialogBindData.validate((valid) => {
        if (valid) {
          if (this.dialogType === 2) {
            member.editorMemberList(param).then(res => {
              if (res.data.code !== 1) {
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                })
                this.buttonState = false
                return false
              }
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.buttonState = false
              this.getList()
              this.handleClose()
            })
          }
        } else {
          return false
        }
      })
    },
    // 修改积分
    integralFun(type) {
      if (type === 0) {
        this.type = 0
      } else {
        this.type = 1
      }
    },
    // 删除
    del(data) {
      if (this.datavalID.length === 0 && data === 0) {
        this.$message({
          type: 'warning',
          message: '请选择商品'
        })
        return false
      }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delList(data)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delList(data) {
      const classID = []
      if (data === 0) {
        for (const val of this.datavalID) {
          classID.push(val.memberId)
        }
      } else {
        classID.push(data.memberId)
      }
      member.delMemberList(classID).then(res => {
        if (res.data.code === 1) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        }
      })
    },
    changeFun(data) {
      this.datavalID = data
    },
    // 拉黑
    shielding(data) {
      this.$confirm('确定拉黑吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = {
          memberId: data.memberId,
          isBlacklist: 1
        }
        member.editorMemberList(param).then(res => {
          if (res.data.code === 1) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
            this.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 恢复
    huifu(data) {
      this.$confirm('确定拉黑吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = {
          memberId: data.memberId,
          isBlacklist: 0
        }
        member.editorMemberList(param).then(res => {
          if (res.data.code === 1) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
            this.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 反显
    reBindData(data) {
      for (const key in this.dialogBindData) {
        this.dialogBindData[key] = data[key]
      }
    },
    // 搜索过滤
    searchData() {
      const arr = []
      for (const key in this.seacherParam) {
        if (this.seacherParam[key] !== '') {
          arr.push('&' + key + '=' + this.seacherParam[key])
        }
      }
      const url = arr.join('')
      return url
    },
    // 重置
    resetFun() {
      this.$refs.seacherParam.resetFields()
      this.pageNo = 1
      this.pageSize = 10
      this.getList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageNo = val
      this.getList()
    },
    handleClose() {
      for (const key in this.dialogBindData) {
        this.dialogBindData[key] = ''
      }
      this.type = ''
      this.dialogState = false
      this.$refs.dialogBindData.clearValidate()
    },
    // 上传头像
    cropSuccess(resData) {
      this.imagecropperShow = false
      this.imagecropperKey = this.imagecropperKey + 1
      this.picUrl = resData.data[0].maxImageUrl
    },
    close() {
      this.imagecropperShow = false
    },
    // 性别
    genderVal(type) {
      if (type === 0) {
        return '未录入'
      } else if (type === 1) {
        return '女'
      } else if (type === 2) {
        return '男'
      }
    },
    // 登录锁定状态
    loginLockStatusVal(type) {
      if (type === 0) {
        return '解锁'
      } else if (type === 1) {
        return '锁定'
      }
    },
    // 是否在黑名单
    isBlacklistVal(type) {
      if (type === 0) {
        return '否'
      } else if (type === 1) {
        return '是'
      }
    }
  }
}
</script>
<style type="text/scss" lang="scss" scoped>
  .uploadImg{
    width: 100px;
    height: 100px;
    border-radius: 50%;
    cursor: pointer;
    position: relative;
    /*border: 1px dashed #dcdfe6;*/
    img{
      display: block;
      border-radius: 50%;
      width: 100%;
      height: 100%;
    }
    .uploadImg_mock{
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: rgba(0,0,0,0);
      transition:background-color 0.35s;
      -webkit-transition: background-color 0.35s;
      i{
        position: absolute;
        top: 50%;
        left: 50%;
        color: #fff;
        font-size: 26px;
        margin-left: -12px;
        margin-top: -16px;
        opacity: 0;
        transition:opacity 0.35s;
        -webkit-transition: opacity 0.35s;
      }
    }
    &:hover .uploadImg_mock{
      background-color: rgba(0,0,0,0.5);
      i{
        opacity: 1;
      }
    }
  }

</style>
